package com.hellobike.haxing.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hellobike.haxing.modules.coupon.service.ICouponService;
import com.hellobike.haxing.modules.order.entity.OrderCoupon;
import com.hellobike.haxing.modules.order.mapper.OrderCouponMapper;
import com.hellobike.haxing.modules.order.service.IOrderCouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description: 订单优惠券
 * @Author: haxing-boot
 * @Date:   2020-02-08
 * @Version: V1.0
 */
@Service
public class OrderCouponServiceImpl extends ServiceImpl<OrderCouponMapper, OrderCoupon> implements IOrderCouponService {
    @Autowired
    private OrderCouponMapper orderCouponMapper;
    @Autowired
    private ICouponService couponService;
    @Override
    public List<OrderCoupon> queryList(Long orderId) {
        return orderCouponMapper.queryList(orderId);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void returnCoupon(Long orderId) {
        QueryWrapper<OrderCoupon> queryWrapper = new QueryWrapper();
        queryWrapper.lambda().eq(OrderCoupon::getOrderId,orderId);
        OrderCoupon orderCoupon= super.getOne(queryWrapper);
        if(orderCoupon!=null){
            //返还个人券
            couponService.returnUserCoupon(orderCoupon.getUserCouponId());
            orderCoupon.setReturnFlag(1);
            super.updateById(orderCoupon);
        }
    }
}
